package com.gzhryc.common.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.gzhryc.common.JavascriptRhinoTools;
import com.gzhryc.common.logging.Logger;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

public class I18nTools {

	static Logger log = Logger.getLogger(I18nTools.class);

	static ThreadLocal<String> CurrentLang = new ThreadLocal<String>();
	
	private static Map<String,Properties> Store = new HashMap<String,Properties>();
	private static Map<String,Map<String,Object>> StoreMap = new HashMap<String,Map<String,Object>>();

	public static void load(String filePath,boolean isJson) throws IOException{
		File file = new File(filePath);
		if(file.exists()){
			if(file.isDirectory()){
				File[] files = file.listFiles();
				if(files != null && files.length > 0){
					for(int i=0;i<files.length;i++){
						File item = files[i];
						if(!item.isDirectory()){
							load(item,isJson);
						}
					}
				}
			}else{
				load(file,isJson);
			}
		}
	}
	
	public static void load(File file,boolean isJson) throws IOException{
		InputStream inStream = new FileInputStream(file);
		String name = FilenameUtils.getBaseName(file.getName());
		if(isJson){
			loadJson(name,inStream);
		}else {
			load(name, inStream);
		}
	}

	public static void loadJson(String lang,InputStream inStream) throws IOException{
		BufferedReader reader = null;
		StringBuffer sbf = new StringBuffer();
		try {
			reader = new BufferedReader(new InputStreamReader(inStream,"UTF-8"));
			String tempStr;
			while ((tempStr = reader.readLine()) != null) {
				if(tempStr.trim().startsWith("//")){
					continue;
				}
				sbf.append(tempStr + "\r\n");
			}
			reader.close();

			String script = sbf.toString();
			Object obj = JavascriptRhinoTools.eval(script + "; return i18nData;");
			if(obj != null && Map.class.isInstance(obj)){
				Map<String,Object> data = Map.class.cast(obj);
				StoreMap.put(lang, data);
			}else {
				log.error("未匹配到Json字符串");
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	public static void load(String lang,InputStream inStream) throws IOException{
		BufferedReader bf = new BufferedReader(new InputStreamReader(inStream,"UTF-8"));
		Properties properties = new Properties();
		properties.load(bf);
		Store.put(lang, properties);
	}

	public static I18n getI18n(String lang){
		Map<String,Object> data = StoreMap.get(lang);
		if(data != null) {
			return new I18n(data);
		}
		Properties properties = Store.get(lang);
		if (properties == null) {
			properties = new Properties();
		}
		return new I18n(properties);
	}
	
	public static String get(String name){
		I18n i18n = getCurrentI18n();
		if(i18n != null) {
			return i18n.get(name);
		}
		return name;
	}
	
	public static String get(String name,String defVal){
		I18n i18n = getCurrentI18n();
		if(i18n != null) {
			return i18n.get(name,defVal);
		}
		return defVal;
	}
	
	public static void setCurrentLang(String lang){
		CurrentLang.set(lang);
	}
	
	public static I18n getCurrentI18n(){
		String lang = CurrentLang.get();
		if(StringUtils.isNotBlank(lang)){
			return getI18n(lang);
		}
		return null;
	}
	
	public static class I18n {
		
		Properties properties;
		Map<String,Object> data;
		
		public I18n(Properties properties){
			this.properties = properties;
		}
		public I18n(Map<String,Object> data){
			this.data = data;
		}
		
		public Map<String,Object> getMap(String name){
			 if(this.data != null){
				 String[] temp = name.split("\\.");
				 if (temp.length > 1) {
					 return getMap(this.data, temp, 1);
				 }else{
					 Object obj = this.data.get(temp[0]);
					 if(obj != null && Map.class.isInstance(obj)){
						 return (Map<String,Object>)obj;
					 }
				 }
			}
			return null;
		}

		public String get(String name){
			if(StringUtils.isNotBlank(name)) {
				if (this.properties != null) {
					String obj =  this.properties.getProperty(name);
					if(StringUtils.isNotBlank(obj)){
						return obj;
					}
				} else if (this.data != null) {
					String[] temp = name.split("\\.");
					if (temp.length > 1) {
						String obj = get(this.data, temp, 1);
						if (obj != null) {
							return obj;
						}
					}else{
						Object obj = this.data.get(temp[0]);
						if (obj != null) {
							return obj.toString();
						}
					}
				}
			}
			return name;
		}

		public String get(String name,String defVal){
			if(StringUtils.isNotBlank(name)) {
				if (this.properties != null) {
					return this.properties.getProperty(name, defVal);
				} else if (this.data != null) {
					String[] temp = name.split("\\.");
					if (temp.length > 1) {
						String obj = get(this.data, temp, 1);
						if (obj != null) {
							return obj;
						}
					} else {
						Object obj = this.data.get(temp[0]);
						if (obj != null) {
							return obj.toString();
						}
					}
				}
			}
			return defVal;
		}

		public Map<String,Object> getData(){
			return this.data;
		}
		
		public Properties getProperties(){
			return this.properties;
		}

		public String get(Map<String,Object> children,String[] name,Integer index){
			Object obj = children.get(name[index]);
			if(obj != null){
				index = index + 1;
				if(name.length == index){
					return obj.toString();
				}else if(Map.class.isInstance(obj)) {
					return get((Map<String, Object>) obj, name, index);
				}
			}
			return null;
		}

		public Map<String,Object> getMap(Map<String,Object> children,String[] name,Integer index){
			Object obj = children.get(name[index]);
			if(obj != null){
				index = index + 1;
				if(Map.class.isInstance(obj)) {
					if(name.length == index){
						return (Map<String, Object>) obj;
					}else{
						return getMap((Map<String, Object>) obj, name, index);
					}
				}
			}
			return null;
		}
	}
}
